<?php
/*
  $Id: ot_giftcard.php,v 1.0 2007/10/23 by Kasey Drexler $
  Email: kdrexler@dyton.com or x@findyourownjob.com
  
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class ot_giftcard {
    var $title, $output;

    function ot_giftcard() {
      $this->code = 'ot_giftcard';
	  $this->title = MODULE_ORDER_TOTAL_GIFTCARD_TITLE;
	  $this->description = MODULE_ORDER_TOTAL_GIFTCARD_DESCRIPTION;
	  $this->enabled = ((MODULE_ORDER_TOTAL_GIFTCARD_STATUS == 'true') ? true : false);
	  $this->sort_order = MODULE_ORDER_TOTAL_GIFTCARD_SORT_ORDER;
	  $this->output = array();
    }

    function process() {
      global $order, $currencies;
	  // check to see if the user has a gift card
	  if (isset($_SESSION['gift_card'])) {
	    $gift_card_query = tep_db_query("select * from `".TABLE_GIFT_CARDS."` where gift_cards_code='".$_SESSION['gift_card']."' and gift_cards_enabled='1'");
		if (tep_db_num_rows($gift_card_query)>0) {
		  $gift_card = tep_db_fetch_array($gift_card_query);
		  
		  // check to see if the user has a valid amount left on gift card
		  $amount_used = 0;
		  if ($gift_card['gift_cards_amount_remaining'] > 0) {
		    if ($order->info['total'] > $gift_card['gift_cards_amount_remaining']) { // if the total is more than gift card amount
			  $amount_used = $gift_card['gift_cards_amount_remaining'];
			  $order->info['total'] -= $gift_card['gift_cards_amount_remaining'];
			  $gift_card['gift_cards_amount_remaining'] = 0;
			} else { // if gift card amount is greater than total
			  $amount_used = $order->info['total'];
			  $gift_card['gift_cards_amount_remaining'] -= $order->info['total'];
			  $order->info['total'] = 0;
			}
		  }
		  // output the amount of the gift card used
		  $this->output[] = array('title' => MODULE_ORDER_TOTAL_GIFTCARD_TITLE . ':',
                                  'text' => '-'.$currencies->format($amount_used, true, $order->info['currency'], $order->info['currency_value']),
                                  'value' => -$amount_used);
		}
	  }
	  
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_GIFTCARD_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
	  
      return $this->_check;
    }

    function keys() {
	  return array('MODULE_ORDER_TOTAL_GIFTCARD_STATUS', 'MODULE_ORDER_TOTAL_GIFTCARD_SORT_ORDER');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display Gift Card Discount', 'MODULE_ORDER_TOTAL_GIFTCARD_STATUS', 'true', 'Do you want to display the discount provided by the gift card entered?', '6', '1','tep_cfg_select_option(array(\'true\', \'false\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_GIFTCARD_SORT_ORDER', '2', 'Sort order of display.', '6', '2', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
  }
?>
